/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package times.persistence.controllers;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import times.persistence.controllers.exceptions.NonexistentEntityException;
import times.persistence.entities.PriceSourceSlaughterHeader;
import times.persistence.entities.Foreas;
import times.persistence.entities.Category;
import times.persistence.entities.PriceSlaughterLamb;

/**
 *
 * @author Administrator
 */
public class PriceSlaughterLambJpaController implements Serializable {

    public PriceSlaughterLambJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(PriceSlaughterLamb priceSlaughterLamb) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            PriceSourceSlaughterHeader priceSourceSlaughterHeaderId = priceSlaughterLamb.getPriceSourceSlaughterHeaderId();
            if (priceSourceSlaughterHeaderId != null) {
                priceSourceSlaughterHeaderId = em.getReference(priceSourceSlaughterHeaderId.getClass(), priceSourceSlaughterHeaderId.getId());
                priceSlaughterLamb.setPriceSourceSlaughterHeaderId(priceSourceSlaughterHeaderId);
            }
            Foreas foreasId = priceSlaughterLamb.getForeasId();
            if (foreasId != null) {
                foreasId = em.getReference(foreasId.getClass(), foreasId.getId());
                priceSlaughterLamb.setForeasId(foreasId);
            }
            Category categoryId = priceSlaughterLamb.getCategoryId();
            if (categoryId != null) {
                categoryId = em.getReference(categoryId.getClass(), categoryId.getId());
                priceSlaughterLamb.setCategoryId(categoryId);
            }
            em.persist(priceSlaughterLamb);
            if (priceSourceSlaughterHeaderId != null) {
                priceSourceSlaughterHeaderId.getPriceSlaughterLambList().add(priceSlaughterLamb);
                priceSourceSlaughterHeaderId = em.merge(priceSourceSlaughterHeaderId);
            }
            if (foreasId != null) {
                foreasId.getPriceSlaughterLambList().add(priceSlaughterLamb);
                foreasId = em.merge(foreasId);
            }
            if (categoryId != null) {
                categoryId.getPriceSlaughterLambList().add(priceSlaughterLamb);
                categoryId = em.merge(categoryId);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(PriceSlaughterLamb priceSlaughterLamb) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            PriceSlaughterLamb persistentPriceSlaughterLamb = em.find(PriceSlaughterLamb.class, priceSlaughterLamb.getId());
            PriceSourceSlaughterHeader priceSourceSlaughterHeaderIdOld = persistentPriceSlaughterLamb.getPriceSourceSlaughterHeaderId();
            PriceSourceSlaughterHeader priceSourceSlaughterHeaderIdNew = priceSlaughterLamb.getPriceSourceSlaughterHeaderId();
            Foreas foreasIdOld = persistentPriceSlaughterLamb.getForeasId();
            Foreas foreasIdNew = priceSlaughterLamb.getForeasId();
            Category categoryIdOld = persistentPriceSlaughterLamb.getCategoryId();
            Category categoryIdNew = priceSlaughterLamb.getCategoryId();
            if (priceSourceSlaughterHeaderIdNew != null) {
                priceSourceSlaughterHeaderIdNew = em.getReference(priceSourceSlaughterHeaderIdNew.getClass(), priceSourceSlaughterHeaderIdNew.getId());
                priceSlaughterLamb.setPriceSourceSlaughterHeaderId(priceSourceSlaughterHeaderIdNew);
            }
            if (foreasIdNew != null) {
                foreasIdNew = em.getReference(foreasIdNew.getClass(), foreasIdNew.getId());
                priceSlaughterLamb.setForeasId(foreasIdNew);
            }
            if (categoryIdNew != null) {
                categoryIdNew = em.getReference(categoryIdNew.getClass(), categoryIdNew.getId());
                priceSlaughterLamb.setCategoryId(categoryIdNew);
            }
            priceSlaughterLamb = em.merge(priceSlaughterLamb);
            if (priceSourceSlaughterHeaderIdOld != null && !priceSourceSlaughterHeaderIdOld.equals(priceSourceSlaughterHeaderIdNew)) {
                priceSourceSlaughterHeaderIdOld.getPriceSlaughterLambList().remove(priceSlaughterLamb);
                priceSourceSlaughterHeaderIdOld = em.merge(priceSourceSlaughterHeaderIdOld);
            }
            if (priceSourceSlaughterHeaderIdNew != null && !priceSourceSlaughterHeaderIdNew.equals(priceSourceSlaughterHeaderIdOld)) {
                priceSourceSlaughterHeaderIdNew.getPriceSlaughterLambList().add(priceSlaughterLamb);
                priceSourceSlaughterHeaderIdNew = em.merge(priceSourceSlaughterHeaderIdNew);
            }
            if (foreasIdOld != null && !foreasIdOld.equals(foreasIdNew)) {
                foreasIdOld.getPriceSlaughterLambList().remove(priceSlaughterLamb);
                foreasIdOld = em.merge(foreasIdOld);
            }
            if (foreasIdNew != null && !foreasIdNew.equals(foreasIdOld)) {
                foreasIdNew.getPriceSlaughterLambList().add(priceSlaughterLamb);
                foreasIdNew = em.merge(foreasIdNew);
            }
            if (categoryIdOld != null && !categoryIdOld.equals(categoryIdNew)) {
                categoryIdOld.getPriceSlaughterLambList().remove(priceSlaughterLamb);
                categoryIdOld = em.merge(categoryIdOld);
            }
            if (categoryIdNew != null && !categoryIdNew.equals(categoryIdOld)) {
                categoryIdNew.getPriceSlaughterLambList().add(priceSlaughterLamb);
                categoryIdNew = em.merge(categoryIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = priceSlaughterLamb.getId();
                if (findPriceSlaughterLamb(id) == null) {
                    throw new NonexistentEntityException("The priceSlaughterLamb with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            PriceSlaughterLamb priceSlaughterLamb;
            try {
                priceSlaughterLamb = em.getReference(PriceSlaughterLamb.class, id);
                priceSlaughterLamb.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The priceSlaughterLamb with id " + id + " no longer exists.", enfe);
            }
            PriceSourceSlaughterHeader priceSourceSlaughterHeaderId = priceSlaughterLamb.getPriceSourceSlaughterHeaderId();
            if (priceSourceSlaughterHeaderId != null) {
                priceSourceSlaughterHeaderId.getPriceSlaughterLambList().remove(priceSlaughterLamb);
                priceSourceSlaughterHeaderId = em.merge(priceSourceSlaughterHeaderId);
            }
            Foreas foreasId = priceSlaughterLamb.getForeasId();
            if (foreasId != null) {
                foreasId.getPriceSlaughterLambList().remove(priceSlaughterLamb);
                foreasId = em.merge(foreasId);
            }
            Category categoryId = priceSlaughterLamb.getCategoryId();
            if (categoryId != null) {
                categoryId.getPriceSlaughterLambList().remove(priceSlaughterLamb);
                categoryId = em.merge(categoryId);
            }
            em.remove(priceSlaughterLamb);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<PriceSlaughterLamb> findPriceSlaughterLambEntities() {
        return findPriceSlaughterLambEntities(true, -1, -1);
    }

    public List<PriceSlaughterLamb> findPriceSlaughterLambEntities(int maxResults, int firstResult) {
        return findPriceSlaughterLambEntities(false, maxResults, firstResult);
    }

    private List<PriceSlaughterLamb> findPriceSlaughterLambEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(PriceSlaughterLamb.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public PriceSlaughterLamb findPriceSlaughterLamb(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(PriceSlaughterLamb.class, id);
        } finally {
            em.close();
        }
    }

    public int getPriceSlaughterLambCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<PriceSlaughterLamb> rt = cq.from(PriceSlaughterLamb.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
